package com.ruoyi.project.module.series.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.project.module.banner.controller.BannerController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.series.domain.Series;
import com.ruoyi.project.module.series.service.ISeriesService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 影视系列 信息操作处理
 * 
 * @author frey
 * @date 2018-09-28
 */
@Controller
@RequestMapping("/module/series")
public class SeriesController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(BannerController.class);
    private String prefix = "module/series";
	
	@Autowired
	private ISeriesService seriesService;

	@Value("${ruoyi.profile}")
	private String uploadUrl;


	@Value("${ruoyi.urlAddress}")
	private String urlAddress;
	
	@RequiresPermissions("module:series:view")
	@GetMapping()
	public String series()
	{
	    return prefix + "/series";
	}
	
	/**
	 * 查询影视系列列表
	 */
	@RequiresPermissions("module:series:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Series series)
	{
		startPage();
        List<Series> list = seriesService.selectSeriesList(series);
		return getDataTable(list);
	}
	
	/**
	 * 新增影视系列
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存影视系列
	 */
	@RequiresPermissions("module:series:add")
	@Log(title = "影视系列", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Series series,@RequestParam(value = "seriesfile",required = false) MultipartFile file)
	{
		try {
			if (!file.isEmpty())
			{
				String carouselUrl = FileUploadUtils.upload(uploadUrl,file);
				series.setImg(urlAddress+carouselUrl);
				return toAjax(seriesService.insertSeries(series));
			}
			return error();
		} catch (IOException e) {
			log.error("上传一键记账轮播图失败！", e);
			return error();
		}

	}

	/**
	 * 修改影视系列
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Series series = seriesService.selectSeriesById(id);

		mmap.put("series", series);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存影视系列
	 */
	@RequiresPermissions("module:series:edit")
	@Log(title = "影视系列", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Series series,@RequestParam(value = "seriesfile",required = false) MultipartFile file)
	{
		try {
			if (file==null)
			{
				return toAjax(seriesService.updateSeries(series));

			}else{
				String carouselUrl = FileUploadUtils.upload(uploadUrl,file);
				series.setImg(urlAddress+carouselUrl);

				return toAjax(seriesService.updateSeries(series));
			}

		} catch (IOException e) {
			log.error("上传一键记账轮播图失败！", e);
			return error();
		}


	}
	
	/**
	 * 删除影视系列
	 */
	@RequiresPermissions("module:series:remove")
	@Log(title = "影视系列", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(seriesService.deleteSeriesByIds(ids));
	}
	
}
